package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modele.Dep;
import modele.Med;
import modele.Pays;
import modele.Spe;

public class Control extends HttpServlet {
    private Pays p;
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
            p = new Pays();
    }
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String page = "index.jsp";
        if(request.getParameter("uc") != null) {
            String uc = request.getParameter("uc");
            switch(uc) {
                case "nom" : page = "nom.jsp";
                if(request.getParameter("action") != null) {
                    String n = request.getParameter("nom");
                    Collection<Med> medn = p.getMedNom(n);
                    if(medn.isEmpty()) {
                        request.setAttribute("nom", n);
                        page = "pasdemed.jsp";
                    } else {
                        request.setAttribute("nom", n);
                        request.setAttribute("meds", medn);
                        page = "medecinsparnom.jsp";
                    }
                }
                    break;
                case "departement" : Collection <Dep> lesDeps = p.getLesDeps();
                    if(request.getParameter("action") != null) {
                        Dep d = p.getLeDep(request.getParameter("dep"));
                        Collection <Med> lesMeds = d.getLesMeds();
                        request.setAttribute("dep", d);
                        request.setAttribute("meds", lesMeds);
                        page = "medecinspardep.jsp";
                    } else {
                        request.setAttribute("deps", lesDeps);
                        page = "departement.jsp";
                    }
                    break;
                    case "specialite" : Collection <Spe> lesSpes = p.getLesSpes();
                    if(request.getParameter("action") != null) {
                        Spe s = p.getLaSpe(request.getParameter("spe"));
                        Collection <Med> lesM = s.getLesMeds();
                        request.setAttribute("spe", s);
                        request.setAttribute("meds", lesM);
                        page = "medecinsparspe.jsp";
                    } else {
                        request.setAttribute("spes", lesSpes);
                        page = "specialite.jsp";
                    }
                    break;
//                case "medecinspardep" : Dep d = p.getLeDep(request.getParameter("dep"));
//                    Collection <Med> lesMeds = d.getLesMeds();
//                    request.setAttribute("dep", d);
//                    request.setAttribute("meds", lesMeds);
//                    page = "medecinspardep.jsp";
//                    break;
//                case "medecinsparnom" : String n = request.getParameter("nom");
//                    Collection<Med> medn = p.getMedNom(n);
//                    if(medn.isEmpty()) {
//                        request.setAttribute("nom", n);
//                        page = "pasdemed.jsp";
//                    } else {
//                        request.setAttribute("meds", medn);
//                        page = "medecinsparnom.jsp";
//                    }
//                    break;
                default : page = "index.jsp";
            }
        }
        request.getRequestDispatcher(page).forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
            processRequest(request, response);
        
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
            processRequest(request, response);
        
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}